package org.csu.mypetstore.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.CartMapper;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CategoryService;
import org.csu.mypetstore.service.impl.CartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/carts")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping
    @ResponseBody
    public String getItems(String username){
        //System.out.println(account.getUsername());
        Account account = new Account();
        account.setUsername(username);
        List<String>items = cartService.getItemsinCart(account);
        JSONArray jsonArray = new JSONArray();
        for(int i = 0 ;i <items.size();i++) {
            String itemId = items.get(i);
            JSONObject jsonObject = cartService.getItemInfo(account,itemId);
            jsonObject.put("itemId",itemId);
            jsonArray.add(jsonObject);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "OK");
        jsonObject.put("data", jsonArray);
        return jsonObject.toString();
    }

    @PostMapping
    @ResponseBody
    public String createItemInCart(@RequestParam("username") String username,@RequestParam("item") String item){
        Account  account = new Account();
        account.setUsername(username);
        if(item == null){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("status","itemId为空");
            return jsonObject.toString();
        }
        cartService.insertItemToCart(account,item);
        List<String>items = cartService.getItemsinCart(account);
        JSONArray jsonArray = new JSONArray();
        for(int i = 0 ;i <items.size();i++) {
            String itemId = items.get(i);
            JSONObject jsonObject = cartService.getItemInfo(account,itemId);
            jsonObject.put("itemId",itemId);
            jsonArray.add(jsonObject);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", "OK");
        jsonObject.put("data", jsonArray);
        return jsonObject.toString();
    }

    @PutMapping
    @ResponseBody
    public String updateItemInCart(@RequestParam("username")String username,@RequestParam("item")String item,@RequestParam("type")int type){
        Account account = new Account();
        account.setUsername(username);
        if(type == 0){
            cartService.insertItemToCart(account,item);
        }
        else{
            cartService.minusCart(account,item);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","OK");
        return jsonObject.toString();
    }

    @DeleteMapping("/{username}")
    @ResponseBody
    public String clearCart(@PathVariable(value = "username")String username){
        Account account = new Account();
        account.setUsername(username);
        cartService.clearUpCart(account);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","OK");
        return jsonObject.toString();
    }

    @DeleteMapping
    @ResponseBody
    public String deleteItem(@RequestParam("username")String username,@RequestParam("item")String item){
        Account account = new Account();
        account.setUsername(username);
        cartService.deleteItemFromCart(account,item);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","OK");
        return jsonObject.toString();
    }

}
